<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:r="http://www.w3.org/1999/XSL/Transform_RESULT"
  xmlns:e="livcos.org/ns/editor"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="job e">
  
  <xsl:output method="xml" encoding="UTF-8"/>
  
  <xsl:param name="objId"/>
  <xsl:param name="scope" select="'content'"/>
  <xsl:param name="id"/>
	<xsl:param name="type"/>
	<xsl:param name="ed"/>
	
	<xsl:param name="_editorBase" select="'/livcos.org/type/editor/'"/>
	<xsl:param name="_contentExt" select="'.content'"/>
	
  <xsl:namespace-alias stylesheet-prefix="r" result-prefix="xsl"/>

  <xsl:template match="/">
    <xsl:apply-templates select="document(concat($_editorBase, $type, $_contentExt))/*"/>
	</xsl:template>
	
	<xsl:template match="e:editor">
	  <job:transform>
  	  <r:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  			<xsl:variable name="ns_nodes" 
  										select="namespace::*[(local-name(.) != 'xml') and (local-name(.) != '')]"/>
  			<xsl:for-each-group group-by="."
  				select="$ns_nodes">
  				<xsl:copy-of select="."/>
  			</xsl:for-each-group>
  			<xsl:attribute name="exclude-result-prefixes">
  			  <xsl:for-each-group group-by="." select="$ns_nodes">
  					<xsl:value-of select="local-name(.)"/>
  					<xsl:text> </xsl:text>
  				</xsl:for-each-group>
  			</xsl:attribute>
  		  
  		  <r:import href="/livcos.org/web/editor/style/SimpleId.xsl"/>
  			
  		  <r:template match="/">
  		    <r:call-template name="applyNode">
  		      <r:with-param name="parentNode" select="document('{$objId}.{$scope}')"/>
  		      <r:with-param name="id" select="'{$id}'"/>
  		    </r:call-template>
  		  </r:template>
  		  
  	    <r:template match="node()|@*" mode="applyNode">
  	      <input type="hidden" name="objId" value="{$objId}" />
  	      <input type="hidden" name="scope" value="{$scope}" />
  	      <xsl:if test="normalize-space($id) != ''">
  	        <input type="hidden" name="id" value="{$id}" />
  	      </xsl:if>
  	      <table class="ed_body">
						<tbody>
							<xsl:variable name="editors" select="e:input | e:editor"/>
							<xsl:choose>
								<xsl:when test="normalize-space($ed) != ''">
									<xsl:apply-templates select="$editors[@id = $ed]/*"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:apply-templates select="$editors[1]/*"/>
								</xsl:otherwise>
							</xsl:choose>
						</tbody>
  	      </table>
  	      <div class="ed_footer">
  	        <div id="_richtextToolbar">&#160;</div>
  	        <xsl:for-each-group select="$ns_nodes" group-by=".">
  	          <input name="ns-{local-name(.)}" value="{.}" type="hidden"/>
  	        </xsl:for-each-group>
  	        <xsl:if test="namespace-uri-for-prefix('', .) = ''">
  	          <input name="ns" value="" type="hidden"/>
  	        </xsl:if>
  	        <input type="submit" name="update" value="Update" class="ed_action"/>
  	        <input type="button" onclick="this.form._panel.close();" value="Cancel" class="ed_action"/>
  	      </div>
  	    </r:template>
  	    
  	    <r:template match="node()" mode="content root_content" priority="0">
  	      <r:copy>
  	        <r:apply-templates select="node()" mode="content" />
  	      </r:copy>
  	    </r:template>
  	    
  	    <r:template match="comment()" mode="content root_content" priority="0.1">
  	      <r:value-of select="."/>
  	    </r:template>
  	    
  	    <r:template match="*" mode="root_content" priority="0.1">
  	      <r:apply-templates select="node()" mode="content" />
  	    </r:template>
  	    
  	    <r:template match="*" mode="content" priority="0.1">
  	      <r:variable name="htmlTag">
  	        <r:if test="namespace-uri(.) = 'livcos.org/ns/doc'">
              <r:choose>
                <r:when test="local-name(.) = 'br'">br</r:when>
                <r:when test="local-name(.) = 'strong'">b</r:when>
                <r:when test="local-name(.) = 'em'">i</r:when>
                <r:when test="local-name(.) = 'u'">u</r:when>
                <r:when test="local-name(.) = 's'">strike</r:when>
                <r:when test="local-name(.) = 'sub'">sub</r:when>
                <r:when test="local-name(.) = 'sup'">sup</r:when>
              </r:choose>
  	        </r:if>
  	      </r:variable>
  	      <r:choose>
  	        <r:when test="$htmlTag != ''">
  	        	<r:element name="{{$htmlTag}}" namespace="http://www.w3.org/1999/xhtml">
  	            <r:apply-templates select="node()" mode="content"/>
  	          </r:element>
  	        </r:when>
  	        <r:otherwise>
  	          <pre style="color: red"><r:apply-templates select="." mode="escape" /></pre>
  	        </r:otherwise>
  	      </r:choose>
  	    </r:template>
  	    
        <r:template mode="content"
          match="*[(local-name(.) = 'link') and (namespace-uri(.) = 'livcos.org/ns/doc')]">
  			  <a id="{{@id}}">
  			    <r:attribute name="href">
  			      <r:choose>
  			        <r:when test="normalize-space(@ref) != ''">
  			          <r:value-of select="@ref" />
  			        </r:when>
  			        <r:when test="normalize-space(@url) != ''">
  			          <r:value-of select="@url" />
  			        </r:when>
  			        <r:otherwise>
  			          <xsl:text>#</xsl:text>
  			        </r:otherwise>
  			      </r:choose>
  			    </r:attribute>
  			    <r:apply-templates select="node()" mode="content"/>
  			  </a>
        </r:template>
  		  
  	    <r:template match="*" mode="root_escape" priority="0.1">
  	      <r:apply-templates select="*" mode="escape">
  	        <r:with-param name="root" select="1"/>
  	      </r:apply-templates>
  	    </r:template>
  	    
  	    <r:template match="@*" mode="escape">
  		    <r:text><xsl:text> </xsl:text></r:text>
  		    <r:value-of select="name(.)" />
  		    <r:text>="</r:text>
  		    <r:value-of select="." />
  		    <r:text>"</r:text>
  		  </r:template>
  		  
  		  <r:template match="*" mode="escape">
  		    <r:param name="root"/>
  		    <r:text><![CDATA[<]]></r:text>
  		    <r:value-of select="name(.)" />
  		    <r:apply-templates select="@*" mode="escape"/>
  		    <r:for-each select="namespace::*[local-name(.) != 'xml']">
  		      <r:variable name="prefix" select="local-name(.)"/>
  		      <r:variable name="nsUri" select="."/>
  		      <r:if test="not(../../namespace::*[(local-name(.) = $prefix) and (. = $nsUri)]) or $root">
  		        <r:text> xmlns</r:text>
  		        <r:if test="$prefix != ''">:</r:if>
  		        <r:value-of select="$prefix" />="<r:value-of select="$nsUri" />
  		        <r:text>"</r:text>
  		      </r:if>
  		    </r:for-each>
  		    <r:choose>
  		      <r:when test="node()">
  		        <r:text><![CDATA[>]]></r:text>
  		        <r:apply-templates select="node()" mode="escape"/>
  		        <r:text><![CDATA[<]]>/</r:text>
  		        <r:value-of select="name(.)" />
  		        <r:text><![CDATA[>]]></r:text>
  		      </r:when>
  		      <r:otherwise>
  		        <r:text>/<![CDATA[>]]></r:text>
  		      </r:otherwise>
  		    </r:choose>
  		  </r:template>
  		  
  		</r:stylesheet>
	  </job:transform>
  </xsl:template>
  
  <xsl:template match="e:info">
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <r:value-of select="{@path}"/>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:text | e:integer">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
			  <!-- overflow:auto div to work around input cursor bug in Firefox -->
			  <div>
			    <input name="content_{$inputCount}" value="{{{@path}}}" type="text"
			      onfocus="this.parentNode.style.overflow = 'auto'"
			      onblur="this.parentNode.style.overflow = 'visible'">
			    	<xsl:if test="@onChange">
							<xsl:attribute name="onkeyup">
								<xsl:text>this.form._panel.ed_</xsl:text>
								<xsl:value-of select="@onChange"/>
								<xsl:text>(this, event, '</xsl:text>
								<xsl:value-of select="@path"/>
								<xsl:text>')</xsl:text>
							</xsl:attribute>
			    	</xsl:if>
			    	<xsl:if test="not((@path = '.') or contains(@path,'title') or contains(@path,'label'))">
			    		<xsl:attribute name="autocomplete">off</xsl:attribute>
			    	</xsl:if>
			    </input>
			  </div>
      </td>
    </tr>
  </xsl:template>
	
	<xsl:template match="e:text[@multiline = '1']">
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
		  <xsl:if test="@label">
  			<td class="ed_label">
  				<xsl:value-of select="@label"/>
  				<xsl:text>:</xsl:text>
  			</td>
		  </xsl:if>
			<td class="ed_entry">
        <xsl:if test="not(@label)">
          <xsl:attribute name="colspan">2</xsl:attribute>
        </xsl:if>
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
			  <xsl:choose>
			    <xsl:when test="@plain = 1">
			      <input name="type_{$inputCount}" value="plain" type="hidden"/>
			      <div style="overflow: auto;">
			        <textarea class="input" name="content_{$inputCount}">
			          <r:apply-templates select="{@path}" mode="root_escape"/>
			        </textarea>
			      </div>
			    </xsl:when>
			    <xsl:otherwise>
			      <div class="richtext_input" name="content_{$inputCount}">
			        <r:apply-templates select="{@path}" mode="root_content"/>
			      </div>
			    </xsl:otherwise>
			  </xsl:choose>
			</td>
		</tr>
	</xsl:template>
	
  <xsl:template match="e:choice">
		<!-- TODO 9: exclusive switch... -->
		<xsl:apply-templates select="e:option/*"/>
	</xsl:template>
	
  <xsl:template match="e:ref | e:url | e:user">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
      	<xsl:choose>
      		<xsl:when test="@text">
      			<r:variable name="val" select="{@text}"/>
      		</xsl:when>
      		<xsl:otherwise>
      			<r:variable name="val" select="{@path}"/>
      		</xsl:otherwise>
      	</xsl:choose>
      	<div class="ref_input">
          <input name="content_{$inputCount}" value="{{$val}}" type="text" autocomplete="off"/>
        </div>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:time">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <table cellspacing="0" cellpadding="0">
          <tr>
            <td>
              <!-- overflow:auto div to work around input cursor bug in Firefox -->
              <div>
                <input name="content_{$inputCount}" value="{{{@path}}}" type="text"
                  onfocus="this.parentNode.style.overflow = 'auto'" onblur="this.parentNode.style.overflow = 'visible'"
                />
              </div>
            </td>
            <td>
              <input type="button" value="now">
                <xsl:attribute name="onclick">
                  <xsl:text>this.form.content_</xsl:text>
                  <xsl:value-of select="$inputCount"/>
                  <xsl:text>.value = (new Date()).format('HH:mm:ss');</xsl:text>
                </xsl:attribute>
              </input>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:date | e:dateTime">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <div class="{local-name()}_input">
          <input name="content_{$inputCount}" value="{{{@path}}}" type="text"/>
        </div>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:percentage">
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<select name="content_{$inputCount}">
				  <option value="">-</option>
				  <xsl:variable name="currentValue" select="@path"/>
					<xsl:for-each select="0 to 10">
						<xsl:variable name="val" select=". * 10"/>
						<option value="{$val}">
						  <r:if test="({$currentValue} != '') and ({$currentValue} = {$val})">
								<r:attribute name="selected">true</r:attribute>
							</r:if>
							<xsl:value-of select="$val"/>
						  <xsl:text>%</xsl:text>
						</option>
					</xsl:for-each>
				</select>
			</td>
		</tr>
	</xsl:template>
	
  <xsl:template match="e:priority">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <select name="content_{$inputCount}">
          <xsl:variable name="currentValue" select="@path"/>
          <xsl:for-each select="1 to 5">
            <option value="{.}">
							<r:if
								test="(({$currentValue} != '') and ({$currentValue} = {.})) or (({$currentValue} = '') and ({.} = 3))">
								<r:attribute name="selected">true</r:attribute>
							</r:if>
              <xsl:value-of select="."/>
              <xsl:choose>
              	<xsl:when test=". = 1"> (critical)</xsl:when>
              	<xsl:when test=". = 2"> (high)</xsl:when>
              	<xsl:when test=". = 3"> (normal)</xsl:when>
              	<xsl:when test=". = 4"> (low)</xsl:when>
              	<xsl:when test=". = 5"> (none)</xsl:when>
              </xsl:choose>
            </option>
          </xsl:for-each>
        </select>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:enum">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <select name="content_{$inputCount}">
          <xsl:variable name="currentValue" select="@path"/>
          <xsl:for-each select="e:option">
            <option value="{@value}">
              <r:if test="({$currentValue} != '') and ({$currentValue} = '{@value}')">
                <r:attribute name="selected">true</r:attribute>
              </r:if>
              <xsl:value-of select="."/>
            </option>
          </xsl:for-each>
        </select>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:boolean">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <input id="content_{$inputCount}" name="content_{$inputCount}" value="{{{@path}}}" type="hidden"/>
        <input type="checkbox" style="width: auto"
          onclick="document.getElementById('content_{$inputCount}').value = (this.checked)?'1':'0'">
          <r:if test="{@path} = '1'">
            <r:attribute name="checked">true</r:attribute>
          </r:if>
        </input>
      </td>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>
